﻿using System;

using BizElements.Core;

namespace BizElements.BusinessLayer
{
    /// <summary>
    /// Field which is retrieved and displayed in the search results retrieved by <see cref="BizElements.BusinessLayer.SearchEngine"/>.
    /// </summary>
    [Serializable]
    public sealed class DisplayItem : SelectItem, IBindableField
    {
        #region Constructors.

        /// <summary>
        /// Initializes a new instance of <b>DisplayItem</b> class.
        /// </summary>
        /// <param name="item">An item retrieved by <b>BizElements.Core.SelectStatement</b>.</param>
        /// <param name="caption">Item caption displayed in UI layer.</param>
        public DisplayItem(SelectItem item, string caption)
            : base()
        {
            CopyFrom(item);
            this.Caption = caption;
        }

        #endregion

        #region Properties.

        /// <summary>
        /// Gets or sets item caption displayed in UI layer.
        /// </summary>
        public string Caption { get; set; }

        /// <summary>
        /// Gets the name/alias of the display item in DataTable/DataView.
        /// </summary>
        public string Name
        {
            get
            {
                switch (this.ItemType)
                {
                    case SqlItemType.Column:
                        return ((IDbColumn)this.Item).Alias;
                    case SqlItemType.Function:
                        return ((Function)this.Item).Alias;
                    case SqlItemType.SubQuery:
                        return ((SelectStatement)this.Item).Alias;
                    default:
                        return null;
                }
            }
        }

        /// <summary>
        /// Gets or sets item ordinal which defines its position in UI.
        /// Default is zero.
        /// </summary>
        public int Ordinal { get; set; }

        /// <summary>
        /// Gets or ses the description of the bound field. <b>Nullable</b>.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the value which indicates whether the field should be hidden from view in the user interface.
        /// </summary>
        public bool IsHidden { get; set; }

        /// <summary>
        /// Gets the name of the item which is used to bind it to a UI control.
        /// </summary>
        public string BindingName
        {
            get { return this.Alias; }
        }

        /// <summary>
        /// Not implemented. Always <b>true</b>.
        /// </summary>
        public bool IsNullable
        {
            get { return true; }
        }

        /// <summary>
        /// Not implemented. Always <b>true</b>.
        /// </summary>
        public bool IsReadOnly
        {
            get { return true; }
        }

        /// <summary>
        /// Not implemented. Always <b>null</b>.
        /// </summary>
        public ICodeList CodeList
        {
            get { return null; }
            set { }
        }

        /// <summary>
        /// Not implemented. Always <b>null</b>.
        /// </summary>
        public string[] SatelliteFields
        {
            get { return null; }
            set { }
        }

        #endregion
    }	
}
